package com.liangsui.mss.controller.doctor;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.liangsui.mss.aspectj.Log;
import com.liangsui.mss.aspectj.enums.BusinessType;
import com.liangsui.mss.component.alipay.AlipayConfig;
import com.liangsui.mss.service.OrderChargeService;
import lombok.extern.log4j.Log4j2;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * PayController
 *
 * @author zhangjiawei
 * @email 1533167396@qq.com
 * @date 2022/12/8 16:23
 */
@RestController
@RequestMapping("pay")
@Log4j2
public class PayController {

    @DubboReference
    private OrderChargeService orderChargeService;

    /**
     * @功能描述:支付宝支付成功后的回调
     * @Description:进行修改传递进来的订单号进行修改该订单的状态
     */
    @PostMapping("callback/{orderId}")
    @Log(title = "支付宝下单成功回调---修改订单状态(已支付)",businessType = BusinessType.UPDATE)
    public void callback(@PathVariable String orderId, HttpServletRequest request) {
        Map<String, String> parameterMap = this.getParameterMap(request);
        String trade_status = parameterMap.get("trade_status");
        System.err.println(trade_status);
        if ("TRADE_SUCCESS".equals(trade_status)) {
            // 支付成功状态进入
            try {
                boolean singVerified = AlipaySignature.rsaCheckV1(parameterMap, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type);
                System.out.println(singVerified);//只有支付宝调用我们系统的接口才能去更新系统订单状态
                System.out.println("验证签名结果:" + singVerified);
                String refund_fee = parameterMap.get("refund_fee");
                // 不为空
                if (StringUtils.isNotBlank(refund_fee)) {
                    // 退费
                    log.info("退费成功,收费订单子ID为:" + parameterMap.get("out_biz_no"));
                } else {
                    // 支付成功的回调
                    log.info("支付成功,平台ID: " + parameterMap.get("trade_no"));
                    log.info("支付成功,支付ID:" + parameterMap.get("out_trade_no"));
                    String trade_no = parameterMap.get("trade_no");
                    // 支付成功 更新 支付收费 和订单状态 (已支付)
                    this.orderChargeService.paySuccess(orderId, trade_no);

                }

            } catch (AlipayApiException e) {
                e.printStackTrace();
                System.out.println("验证签名出现异常");
            }

            //验证是否为支付宝发来的信息
            System.err.println(JSON.toJSONString(parameterMap));
            System.err.println(trade_status);
            System.err.println(parameterMap);
        } else {
            // 扫码支付中
            System.out.println("扫码成功,等待支付...........................");
            System.out.println("扫码成功,等待支付...........................");
            System.out.println("扫码成功,等待支付...........................");
        }
    }

    /**
     * 获取request中的参数集合转Map
     * Map<String,String> parameterMap = RequestUtil.getParameterMap(request)
     *
     * @param request
     * @return
     */
    public Map<String, String> getParameterMap(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = paramNames.nextElement();
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() != 0) {
                    map.put(paramName, paramValue);
                }
            }
        }
        return map;
    }
}
